package com.tangyuan.niochat.server;

import java.util.Date;

import com.tangyuan.niochat.protocol.Packet;
import com.tangyuan.niochat.protocol.PacketCode;
import com.tangyuan.niochat.protocol.request.LoginRequestPacket;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created on 2019-08-28
 *
 * @author fenghongyu
 */
public class ChatServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println(new Date() + ": 服务端读到数据 ");
        ByteBuf byteBuf = (ByteBuf) msg;
        Packet packet = PacketCode.INSTANCE.decode(byteBuf);
        packetHandler(packet);
    }

    /**
     * 接收数据包解析器
     * @param packet
     */
    private void packetHandler(Packet packet) {
        if(packet instanceof LoginRequestPacket) {
            //登录解析包
            LoginRequestPacket lrp = (LoginRequestPacket)packet;
            System.out.println(lrp.getUserId());
            System.out.println(lrp.getPassWord());
        }
    }
}
